# llvm 目录
#  /zion0/git_proj/llvm-project-llvmorg-18.1.0/

mkdir -p  /zion0/var/log/ccache
rm -rf /zion0/ccache_dir/l2_nvfs_distcc_ccache_build_llvm || echo 0
mkdir -p  /zion0/ccache_dir/l2_nvfs_distcc_ccache_build_llvm

rm -rf /zion0/git_proj/l2_nvfs_distcc_ccache_build_llvm || echo 0
mkdir -p /zion0/git_proj/l2_nvfs_distcc_ccache_build_llvm

mkdir -p /zion0/etc/ssh
cat > /zion0/etc/ssh/ssh_config <<"EOF"
Host *
  StrictHostKeyChecking no
EOF

cat > /zion0/git_proj/l2_nvfs_distcc_ccache_build_llvm/config_ccache.sh << "EOF"
ccache -o 'base_dir=/zion0/git_proj'
ccache -o 'cache_dir=/zion0/ccache_dir/l2_nvfs_distcc_ccache_build_llvm'
ccache -o 'remote_storage='
ccache -o 'remote_only=false'
ccache -o 'recache=false'
ccache -o 'sloppiness=file_macro,include_file_mtime,time_macros,random_seed,include_file_ctime'
ccache -o 'debug=false'
ccache -o 'debug_dir=/zion0/var/log/ccache/l2_nvfs_distcc_ccache_build_llvm/'

ccache -p
EOF

docker run --rm \
    -v /root/.ssh:/root/.ssh \
    -v /root/.config:/root/.config \
    -v /zion0/git_proj:/zion0/git_proj \
    -v /zion0/var/log/ccache:/zion0/var/log/ccache \
    -v /zion0/ccache_dir:/zion0/ccache_dir \
    openeuler/openeuler:22.03-lts-sp2-llvm-ccache-distcc \
    bash /zion0/git_proj/l2_nvfs_distcc_ccache_build_llvm/config_ccache.sh

cat > /zion0/git_proj/l2_nvfs_distcc_ccache_build_llvm.sh << "EOF"
set -eux

echo "172.16.2.123 mstor02" >> /etc/hosts

# DISTCC_POTENTIAL_HOSTS
export DISTCC_HOSTS="localhost mstor02"
export CCACHE_PREFIX=/usr/local/bin/distcc

build_core=100

build_base='/zion0/git_proj/l2_nvfs_distcc_ccache_build_llvm'

build_llvm=${build_base}/build-llvm
build_clang=${build_base}/build-clang
installprefix=${build_base}/install
llvm=/zion0/git_proj/llvm-project-llvmorg-18.1.0/

rm -rf $build_llvm || echo 0
mkdir -p $build_llvm
mkdir -p $installprefix

ccache -z

time cmake \
    -G Ninja -S $llvm/llvm -B $build_llvm \
    -D CMAKE_C_COMPILER_LAUNCHER=ccache -D CMAKE_CXX_COMPILER_LAUNCHER=ccache \
    -DLLVM_INSTALL_UTILS=ON \
    -DCMAKE_INSTALL_PREFIX=$installprefix \
    -DCMAKE_BUILD_TYPE=Release  2>&1 | tee ${build_base}/build.log

time ninja -v -C $build_llvm  -j${build_core} 2>&1 | tee ${build_base}/ninja_build.log

ccache -s

# run with cache
rm -rf $build_llvm || echo 0
mkdir -p $build_llvm
mkdir -p $installprefix

ccache -z

time cmake \
    -G Ninja -S $llvm/llvm -B $build_llvm \
    -D CMAKE_C_COMPILER_LAUNCHER=ccache -D CMAKE_CXX_COMPILER_LAUNCHER=ccache \
    -DLLVM_INSTALL_UTILS=ON \
    -DCMAKE_INSTALL_PREFIX=$installprefix \
    -DCMAKE_BUILD_TYPE=Release  2>&1 | tee ${build_base}/build.log

time ninja -v -C $build_llvm  -j${build_core} 2>&1 | tee ${build_base}/ninja_build.log

ccache -s

EOF


mkdir -p /zion0/l2build_log
chmod 777  /zion0/l2build_log

cat > /zion0/git_proj/docker_build.sh <<"EOF"
docker run --rm \
    --network host \
    -v /zion0/etc/ssh/ssh_config:/etc/ssh/ssh_config:ro \
    -v /root/.ssh:/root/.ssh \
    -v /root/.config:/root/.config \
    -v /zion0/git_proj:/zion0/git_proj \
    -v /zion0/var/log/ccache:/zion0/var/log/ccache \
    -v /zion0/ccache_dir:/zion0/ccache_dir \
    -v /zion0/var/log/ccache:/zion0/var/log/ccache \
    -v  /zion0/l2build_log:/var/log \
    openeuler/openeuler:22.03-lts-sp2-llvm-ccache-distcc \
    bash /zion0/git_proj/l2_nvfs_distcc_ccache_build_llvm.sh
EOF


log_file='/zion0/git_proj/l2_nvfs_distcc_ccache_build_llvm.log'

nohup   bash /zion0/git_proj/docker_build.sh 2>&1 > ${log_file} &

tail -f ${log_file}

#  du -sh /zion0/ccache_dir/l2_nvfs_distcc_ccache_build_llvm

#  grep -A10 -B10 'Cacheable calls' /zion0/git_proj/l2_nvfs_distcc_ccache_build_llvm.sh

# docker run -it  --rm \
#     --network host \
#     -v /root/.ssh:/root/.ssh \
#     -v /root/.config:/root/.config \
#     -v /zion0/git_proj:/zion0/git_proj \
#     -v /zion0/var/log/ccache:/zion0/var/log/ccache \
#     -v /zion0/ccache_dir:/zion0/ccache_dir \
#     openeuler/openeuler:22.03-lts-sp2-llvm-ccache-distcc \
#     bash
